package com.briup.pts.web.controller;

import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.QuarantineRegistration;
import com.briup.pts.service.QrService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "质检管理模块")
@Slf4j
//在COntroller类上添加@Validated注解

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/QuarantineRegistration")
public class QrController {

    private final QrService service;

    @Operation(summary = "分页查询质检报告信息")
    @GetMapping("/query")
    public Result<?> pageQuery(
            @NotNull(message = "分页参数不能为空")
            @Min(value = 1L, message = "当前页面不能小于1") Integer pageNum,
            @Min(value = 1L, message = "页面大小不能小于1")
            @NotNull(message = "分页参数不能为空") Integer pageSize,
            String bQualified, String grMechanism) {
        log.info("分页参数为:{},{}",pageNum,pageSize);

        PageInfo<QuarantineRegistration> pageInfo =
                service.pageQueryByCondition(pageNum, pageSize, bQualified, grMechanism);
        return Result.success(pageInfo)
                .apply("获取成功");
    }

    // 在方法参数对象前标注 @Valid 或 @Validation注解表示该对象属性需要进行参数校验
    @Operation(summary = "新增或更新检疫记录")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(
            @Valid @RequestBody QuarantineRegistration quarantineRegistration) {
        service.saveOrUpdate(quarantineRegistration);
        return Result.success(null)
                .apply("操作成功");
    }

    // 单删
    @Operation(summary = "根据检疫id删除检疫记录")
    @DeleteMapping("/deleteById/{grId}")
    public Result<?> deleteById(
        @NotNull(message = "质检id必须存在")
        @Min(value = 1L,message = "质检id必须有效")
        @PathVariable Integer grId){
        service.deleteById(grId);
        return Result.success(null)
                .apply("用户数据已删除");
    }

    // 批量删除，id数组是通过json格式字符串传输的数据，在请求体中获取
    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(
            @NotEmpty(message = "质检id必须传输")
            @RequestBody List<Integer> grIds) {
        log.info("需要删除的数据为:{}", grIds);
        // 批量删除数据
        service.deleteByIdAll(grIds);
        return Result.success(null)
                .apply("批量删除成功");
    }

}
